/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;
import java.util.Date;

import org.easypr.organizer.client.presenter.BrowsePresenter;
import org.easypr.organizer.shared.MyContact;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class MenuView.
 */
public class BrowseView extends Composite implements BrowsePresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The page. */
	int page = 0;
	
	/** The previous link. */
	Anchor previousLink;
	
	/** The next link. */
	Anchor nextLink;
	
	/** The contacts. */
	ArrayList<MyContact> contacts;
	
	/** The table. */
	FlexTable table;
	/**
	 * Instantiates a new menu view.
	 */
	public BrowseView() {
		container = new FlowPanel();
		initWidget(container);
		previousLink = new Anchor("< Previous");
		nextLink = new Anchor("Next >");
		table = new FlexTable();
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#getContentTable()
	 */
	public FlexTable getContentTable() {
		return this.table;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#getPreviousLink()
	 */
	public Anchor getPreviousLink() {
		return this.previousLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#getNextLink()
	 */
	public Anchor getNextLink() {
		return this.nextLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#increasePage()
	 */
	public void increasePage() {
		this.page++;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#decreasePage()
	 */
	public void decreasePage() {
		this.page--;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#setContacts(java.util.ArrayList)
	 */
	public void setContacts(ArrayList<MyContact> contacts) {
		this.contacts = contacts;
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#setData()
	 */
	public void setData() {
		container.clear();

		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		FlowPanel body = new FlowPanel();
		body.setStyleName("content-body");

		Label lbl = new Label("Browse your contacts");
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		
		
		HorizontalPanel filterContainer = new HorizontalPanel();
		filterContainer.add(new Label("Filter on: "));
		String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ";
		for(int i = 0; i<letters.length(); i++) {
			Hyperlink link = new Hyperlink(letters.substring(i, i+1), "browse/"+letters.substring(i, i+1));
			link.addStyleName("browse-filter");
			filterContainer.add(link);
		}
		body.add(filterContainer);
		
		
		HorizontalPanel paginate = new HorizontalPanel();
		paginate.setStyleName("paginate");
		FlowPanel previous = new FlowPanel();
		previous.setStyleName("paginate-prev");
		FlowPanel next = new FlowPanel();
		next.setStyleName("paginate-next");
		Date date = new Date();
		long iTimeStamp = date.getTime();
		if(contacts != null && contacts.size() > 0) {
			if(page > 0) {
				previous.add(previousLink);
			}
			if(contacts.size() > (this.page+1)*20) {
				next.add(nextLink);
			}
			
			
			paginate.add(previous);
			paginate.add(next);
			
			
			
			int cell = 0;
			int row = 0;
			for(int i=20*this.page; i<contacts.size(); i++) {
				
				FlowPanel item = new FlowPanel();
				item.addStyleName("contact-item");
				
				/*
				Label name = new Label(contacts.get(i).getName());
				name.addStyleName("contact-name");
				item.add(name);
				*/
				//Anchor link = new Anchor(contacts.get(i).getName());
				Hyperlink link = new Hyperlink(contacts.get(i).getName(), "contact/"+contacts.get(i).getId());
				link.addStyleName("contact-name");
				link.setTitle(contacts.get(i).getName());
				item.add(link);
				
				if(contacts.get(i).getEmail().length() > 0) {
					item.add(new Label(contacts.get(i).getEmail()));
				}
				if(contacts.get(i).getCellphone().length() > 0) {
					item.add(new Label("Cell: " + contacts.get(i).getCellphone()));
				}
				if(contacts.get(i).getWorkphone().length() > 0) {
					item.add(new Label("Workphone: " + contacts.get(i).getWorkphone()));
				}
				if(contacts.get(i).getWorkplace().length() > 0) {
					item.add(new Label("Workplace: " + contacts.get(i).getWorkplace()));
				}
				
				if(i == 10) {
					cell++;
					row = 0;
				}
				table.getCellFormatter().setVerticalAlignment(row, cell, HasVerticalAlignment.ALIGN_TOP);
				table.setWidget(row, cell, item);
				//table.getCellFormatter().getElement(row, cell).setId(Long.toString(contacts.get(i).getId()));
				row++;
				
				if(i == 20*this.page+20-1) {
					break;
				}
			}
			
			body.add(paginate);
			
		} else {
			table.setWidget(0, 0, new Label("No contacts found"));
		}
		Date edate = new Date();
        long eTimeStamp = edate.getTime();
        System.out.println(eTimeStamp-iTimeStamp);
		body.add(table);
		container.add(body);
		
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.BrowsePresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}
}
